package sdkshell.kuaiyan.com.kuaiyansdk.http;

import android.text.TextUtils;

import java.util.Map;

import sdkshell.kuaiyan.com.kuaiyansdk.http.Ihttp.NetBuilderConfig;
import sdkshell.kuaiyan.com.kuaiyansdk.http.callback.ResultCallback;
import sdkshell.kuaiyan.com.kuaiyansdk.http.enums.ProtrolType;
import sdkshell.kuaiyan.com.kuaiyansdk.http.enums.RequestType;
import sdkshell.kuaiyan.com.kuaiyansdk.listener.HttpGetInterface;
import sdkshell.kuaiyan.com.kuaiyansdk.listener.HttpPostInterface;

/**
 * Created by Max on 2016/6/23.
 */
public class NumCountHttp {
    private static void postData(NetBuilderConfig config, Map<String, String> params, ResultCallback callback, String url) {
        if(config.getNetConfig().getProtrol() == ProtrolType.HTTP) {
            HttpPostInterface httpPostInterface = new HttpPostSend();
            httpPostInterface.postData(config, params, callback, url);
        }
    }
    private static void getData(NetBuilderConfig config, Map<String, String> params, ResultCallback callback, String url){
        if(config.getNetConfig().getProtrol() == ProtrolType.HTTP) {
            HttpGetInterface httpGetInterface = new HttpGetSend();
            httpGetInterface.getData(config, params, callback, url);
        }
    }
    private static void selectMethod(NetBuilderConfig config, RequestType type, Map<String, String> params, ResultCallback callback, String url) {
        type = config.getNetConfig().getType();
        switch (type){
            case POST:
                postData(config,params, callback, url);
                break;
            case GET:
                getData(config,params,callback,url);
                break;
            default:
                getData(config,params,callback,url);
        }
    }
    /**
     * 通用网络请求
     * @param config
     * @param type
     * @param params
     * @param callback
     */
    public static void requestNetInfo(NetBuilderConfig config, RequestType type, Map<String,String> params, ResultCallback callback){
        String url = config.getNetConfig().getUrl();
        if(TextUtils.isEmpty(url)){
            throw new RuntimeException("网络连接不能为空,请先调用NetBuilderConfig.setUrl方法");
        }
        selectMethod(config, type, params, callback, url);
    }

    public static void download(NetBuilderConfig config, RequestType type, Map<String,String> params, ResultCallback callback) {
        String url = config.getNetConfig().getUrl();
        if(TextUtils.isEmpty(url)){
            throw new RuntimeException("网络连接不能为空,请先调用NetBuilderConfig.setUrl方法");
        }
        selectMethod(config, type, params, callback, url);
    }

    public static void upload(NetBuilderConfig config, RequestType type, Map<String,String> params, ResultCallback callback) {
        String url = config.getNetConfig().getUrl();
        if(TextUtils.isEmpty(url)){
            throw new RuntimeException("网络连接不能为空,请先调用NetBuilderConfig.setUrl方法");
        }
        selectMethod(config, type, params, callback, url);
    }
}
